﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RentalIService;
using RentalDTO;

namespace RentalManagement.Controllers
{
    public class ProcessItemController : Controller
    {
        private readonly IServiceProcessItem item;
        private readonly IServiceStaff staff;
        private readonly IServiceProcessTemplate late;
        public ProcessItemController(IServiceProcessItem _item, IServiceStaff _staff, IServiceProcessTemplate _late)
        {
            item = _item;
            staff = _staff;
            late = _late;
        }
        // GET: ProcessItem
        public ActionResult Index()
        {
            ViewBag.staff = staff.SelectOneList("");
            return View();
        }
        public ActionResult ShowPageList(int limit, int offset, string Name, string Code, string sort, string order)
        {
            if (sort == null)
            {
                sort = "Id";
            }
            int count = 0;
            var list = item.ListPage(limit, offset, Name, Code, ref count, sort, order);
            return Json(new { total = count, rows = list }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Add_up(string Name, string Code, string Id, string strid)
        {
            int count = 0;
            string id="";
            string[] strId = strid.Split(',');
            if (Id == "")
            {
                id = Guid.NewGuid().ToString();
                //添加模板
                count +=item.AddProcessItem(id, Name, Code, 1);
            }
            else
            {
                id = Id;
                //删除之前的步骤
                count += late.DelProcessTemplate(id);
            }

            if (strid != "")
            {
                int a = 1;
                //添加步骤
                foreach (var item in strId)
                {
                    count += late.AddProcessTemplate(Guid.NewGuid().ToString(), id, "无", a, item, DateTime.Now, DateTime.Now, 1);
                    a++;
                }
            }

            return Json(count,JsonRequestBehavior.AllowGet);
        }
        public ActionResult Del(string Id)
        {
            int count = 0;
            string[] id = Id.Split(',');
            foreach (var a in id)
            {
                count+=item.DelProcessItem(a);
                count+= late.DelProcessTemplate(a);
            }
            if (count == id.Length * 2)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }
            
        }
        public ActionResult selectUp(string id)
        {
            return Json(late.select(id), JsonRequestBehavior.AllowGet);
        }
    }
}